package cn.edu.neusoft.practice.Controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.mysql.cj.xdevapi.Result;


import cn.edu.neusoft.practice.Service.DepartmentService;
import cn.edu.neusoft.practice.mapper.DepartmentMapper;
import cn.edu.neusoft.practice.po.Department;

@CrossOrigin
@Controller
public class DepartmentController {

	@Autowired
	private DepartmentMapper departmentMapper;
	
	@RequestMapping("/getAllDepartments")
	@ResponseBody
    public List<Department> getAllDepartments(){
	      QueryWrapper<Department> wrapper1=new QueryWrapper<Department>() ;
	      wrapper1.eq("level",1);
	      List<Department> list1=departmentMapper.selectList(wrapper1);
	      QueryWrapper<Department> wrapper2=new QueryWrapper<Department>() ;
	      wrapper1.eq("level",2);
	      List<Department> list2=departmentMapper.selectList(wrapper2);
	      for(Department d1:list1){
	        List<Department> children=new ArrayList<Department>();
	        for(Department d2:list2)
	        {
	          if(d1.getDepartmentId()==d2.getParentId())
	          {
	            children.add(d2);
	          }

	        }
	        d1.setChildren(children);
	      }
	      return list1;
    }
	/*
	public List<Department> getAllDepartments(){
		
		 return departmentMapper.selectList(null) ;//针对于单表的查询方法
		 
	}
	*/
	
	@RequestMapping("/getDepartsLevel1") //排班
	@ResponseBody
	  public List<Department> getDepartsLevel1(){
	    QueryWrapper<Department> wrapper=new QueryWrapper<Department>() ;
	     wrapper.eq("level",1);
	    List<Department> list=departmentMapper.selectList(wrapper);
	    //return Result.ok(list);
	    return list;
	  }
	
	@RequestMapping("/updateDepartment")
	   @ResponseBody
	    public int updateDepartment(Department d){
	      return departmentMapper.updateById(d);

	    }
	    @RequestMapping("/addDepartment")
	    @ResponseBody
	    public int addDepartment(Department department){
	      return departmentMapper.insert(department);
	    }

	    @RequestMapping("/deleteDepartment")
	    @ResponseBody
	    public int deleDepartment(int departmentId){
	      return departmentMapper.deleteById(departmentId);
	    }
}
